package com.dindang.propertyfee.mvp.presenter;

import android.app.Application;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;

import com.dindang.propertyfee.mvp.model.entity.Payment;
import com.dindang.propertyfee.mvp.model.entity.PropertyCharge;
import com.dindang.propertyfee.mvp.ui.activity.QrCodeCollectionActivity;
import com.dindang.propertyfee.mvp.ui.adapter.custom.FeeAdapter;
import com.dindang.propertyfee.mvp.ui.widget.PaymentSelectDialog;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;

import com.dindang.propertyfee.mvp.contract.FeeListContract;
import com.jess.arms.utils.ArmsUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 废除
 */
@ActivityScope
public class FeeListPresenter extends BasePresenter<FeeListContract.Model, FeeListContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    private FeeAdapter mAdapter;
    private List<PropertyCharge.Total> mList = new ArrayList<>();
    private List<PropertyCharge.CostItem> shoukuan = new ArrayList<>();//存储需要收款的数据

    @Inject
    public FeeListPresenter(FeeListContract.Model model, FeeListContract.View rootView) {
        super(model, rootView);
    }

    public void init() {
        initView();
        initListener();
        getData();
    }

    private void initView() {
        mRootView.getListView().setNestedScrollingEnabled(false);
        mRootView.getListView().setFocusable(false);
        mRootView.getListView().setGroupIndicator(null); //设置属性 GroupIndicator 去掉向下箭头
        mAdapter = new FeeAdapter(mList, mRootView.getActivity(), mListener);
        mRootView.getListView().setAdapter(mAdapter);
    }

    private void initListener() {
        //  设置分组项的点击监听事件
        mRootView.getListView().setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                // 请务必返回 false，否则分组不会展开
                return false;
            }
        });

        //  设置子选项点击监听事件
        mRootView.getListView().setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                return true;
            }
        });

        mRootView.getTvCommit().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogPayment();
            }
        });
    }

    private void getData() {
        for (int i = 1; i <= 10; i++) {
            PropertyCharge.Total total = new PropertyCharge.Total(
                    "丁当社区1幢1单元101室-" + i,
                    i + 100,
                    "张" + i,
                    "1866871993" + i);
            List<PropertyCharge.CostItem> costItems = new ArrayList<>();
            for (int y = 1; y <= 2; y++) {

                PropertyCharge.CostItem costItem = new PropertyCharge.CostItem(
                        "费" + i + y + "项",
                        y + 100.00);
                costItem.setCostItemDetail(i,
                        y + "单位",
                        y,
                        y + y,
                        y + "00元/单位",
                        "201" + y,
                        "201" + (y + y));
                costItems.add(costItem);
            }

            total.costItems = costItems;
            mList.add(total);
        }
        mAdapter.notifyDataSetChanged();
    }

    private int mGroupPosition = 0;
    /**
     * 点击子类 如果父类和当前点击的子类不一样 将原先点击的子类和父类点击都取消
     * 如果一样就不变 其它的看看就懂 毫无技术可言
     */
    private FeeAdapter.OnFeeAdapterListener mListener = new FeeAdapter.OnFeeAdapterListener() {
        @Override
        public void onClickChild(int groupPosition, int childPosition) {

            if(mGroupPosition != groupPosition){
                shoukuan.clear();
                PropertyCharge.Total total = mList.get(mGroupPosition);
                total.isCheck = false;
                total.setCostItems(false);
            }
            mGroupPosition = groupPosition;

            PropertyCharge.CostItem costItem = mList.get(groupPosition).costItems.get(childPosition);
            if(costItem.isCheck) {
                costItem.isCheck = false;
                shoukuan.remove(costItem);
            }else{
                costItem.isCheck = true;
                shoukuan.add(costItem);
            }
            for (PropertyCharge.CostItem ci : mList.get(groupPosition).costItems){
                if(!ci.isCheck){
                    mList.get(groupPosition).isCheck = false;
                    mAdapter.notifyDataSetChanged();
                    return;
                }
            }
            mList.get(groupPosition).isCheck = true;
            mAdapter.notifyDataSetChanged();

        }

        @Override
        public void onClickGroup(int groupPosition) {
            if(mGroupPosition != groupPosition){
                shoukuan.clear();
                PropertyCharge.Total total = mList.get(mGroupPosition);
                total.isCheck = false;
                total.setCostItems(false);
            }
            mGroupPosition = groupPosition;

            PropertyCharge.Total total = mList.get(groupPosition);
            for (PropertyCharge.Total t : mList){
                if(t == total){
                    t.isCheck = !total.isCheck;
                    t.setCostItems(total.isCheck, shoukuan);
                }else{
                    if(t.isCheck){
                        t.isCheck = false;
                        t.setCostItems(false);
                    }
                }
            }

            mAdapter.notifyDataSetChanged();
        }
    };

    PaymentSelectDialog.Builder mPaymentSelectDialog;
    private void dialogPayment(){
        if(shoukuan.size() == 0){
            ArmsUtils.makeText(mApplication, "请选择费项");
            return;
        }

        if(mPaymentSelectDialog != null){
            mPaymentSelectDialog.show();
        }else{
            mPaymentSelectDialog = new PaymentSelectDialog.Builder(mRootView.getActivity());
            mPaymentSelectDialog.setTitle("请选择支付方式")
            .setProperty(new PaymentSelectDialog.OnPaymentListener() {
                @Override
                public void onClick(String paytype) {
                    ArmsUtils.makeText(mApplication, paytype + "二维码生成");
                    Intent intent = new Intent(mRootView.getActivity(), QrCodeCollectionActivity.class);
                    mRootView.getActivity().startActivity(intent);
                }

                @Override
                public void onClick(String paytype, String name) {

                }

                @Override
                public void onHidden(EditText editText) {

                }
            })
            .create()
            .show();
            mPaymentSelectDialog.updateData();
        }
//        mPaymentSelectDialog.updateData(shoukuan);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;

        this.mAdapter = null;
        this.mListener = null;
        this.mList = null;
        this.shoukuan = null;

        this.mPaymentSelectDialog = null;
    }
}
